﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AD.Model;
using AD.BLL;

public partial class Manager_Ad_KeywordsList : KeyWordsPageBase
{
    public List<KeyWordsMod> KeyWords = new List<KeyWordsMod>();
    public int currPageIndex = 1;
    public int MaxSize = 0;
    public int MaxPage = 0;
    public int MaxOnLine = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        string indexstr = Request.QueryString["pageIndex"];
        string action = Request.QueryString["action"];
        string idstr = Request.QueryString["id"];
        MaxOnLine = KeyWordsBLL.SumFilter(" IsUse=1 and Status=1 and UserId=" + SessionUser.AutoID);
        if (!string.IsNullOrEmpty(action))
        {
            bool effect = int.TryParse(indexstr, out currPageIndex);
            int id = 0;
            int.TryParse(idstr, out id);
            if (action.Equals("queryFilter") && effect == true)
            {
                SetDataBind(UtilHepler.KEYWORD_PAGE_SIZE, 1);
            }
            else if (action.Equals("delete") && id > 0)
            {
                KeyWordsBLL.Delete(id);
                SetDataBind(UtilHepler.KEYWORD_PAGE_SIZE, currPageIndex);
            }
            else if (action.Equals("gopage") && effect == true)
            {
                SetDataBind(UtilHepler.KEYWORD_PAGE_SIZE, currPageIndex);
            }
            else if (action.Equals("status") && id > 0)
            {
                KeyWordsMod keywords = KeyWordsBLL.QueryById(idstr);
                if (keywords != null)
                {
                    //KeyWordsBLL.UpdateAudit(!keywords.Status, keywords.AutoID);
                    SetDataBind(UtilHepler.KEYWORD_PAGE_SIZE, currPageIndex);
                }
            }
            else if (action.Equals("updateuse"))
            {
                int errcode = 0;
                KeyWordsBLL.UpdateUse(idstr,out errcode);
                SetDataBind(UtilHepler.KEYWORD_PAGE_SIZE, currPageIndex);
                if (errcode == -2)
                    Alert("一天只能修改一次!");
            }
        }
    }

    void SetDataBind(int pageSize, int index)
    {

        KeyWords = KeyWordsBLL.QueryByPage(" UserId=" + SessionUser.AutoID, pageSize, index == 0 ? 0 : index - 1, out MaxSize);
        MaxPage = MaxSize / UtilHepler.PAGE_SIZE;
        if (MaxSize % UtilHepler.PAGE_SIZE != 0)
            MaxPage += 1;
        if (KeyWords == null)
            KeyWords = new List<KeyWordsMod>();
    }
}